package com.java110.mall.cart.smo.impl;


import com.java110.mall.cart.dao.IInoutOrderProductServiceDao;
import com.java110.core.base.smo.BaseServiceSMO;
import com.java110.dto.PageDto;
import com.java110.dto.inoutOrder.InoutOrderProductDto;
import com.java110.intf.cart.IInoutOrderProductInnerServiceSMO;
import com.java110.po.inoutOrder.InoutOrderProductPo;
import com.java110.utils.util.BeanConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName FloorInnerServiceSMOImpl
 * @Description 出入库商品内部服务实现类
 * @Author wuxw
 * @Date 2019/4/24 9:20
 * @Version 1.0
 * add by wuxw 2019/4/24
 **/
@RestController
public class InoutOrderProductInnerServiceSMOImpl extends BaseServiceSMO implements IInoutOrderProductInnerServiceSMO {

    @Autowired
    private IInoutOrderProductServiceDao inoutOrderProductServiceDaoImpl;


    @Override
    public int saveInoutOrderProduct(@RequestBody InoutOrderProductPo inoutOrderProductPo) {
        int saveFlag = 1;
        inoutOrderProductServiceDaoImpl.saveInoutOrderProductInfo(BeanConvertUtil.beanCovertMap(inoutOrderProductPo));
        return saveFlag;
    }

    @Override
    public int updateInoutOrderProduct(@RequestBody InoutOrderProductPo inoutOrderProductPo) {
        int saveFlag = 1;
        inoutOrderProductServiceDaoImpl.updateInoutOrderProductInfo(BeanConvertUtil.beanCovertMap(inoutOrderProductPo));
        return saveFlag;
    }

    @Override
    public int deleteInoutOrderProduct(@RequestBody InoutOrderProductPo inoutOrderProductPo) {
        int saveFlag = 1;
        inoutOrderProductPo.setStatusCd("1");
        inoutOrderProductServiceDaoImpl.updateInoutOrderProductInfo(BeanConvertUtil.beanCovertMap(inoutOrderProductPo));
        return saveFlag;
    }

    @Override
    public List<InoutOrderProductDto> queryInoutOrderProducts(@RequestBody InoutOrderProductDto inoutOrderProductDto) {

        //校验是否传了 分页信息

        int page = inoutOrderProductDto.getPage();

        if (page != PageDto.DEFAULT_PAGE) {
            inoutOrderProductDto.setPage((page - 1) * inoutOrderProductDto.getRow());
        }

        List<InoutOrderProductDto> inoutOrderProducts = BeanConvertUtil.covertBeanList(
                inoutOrderProductServiceDaoImpl.getInoutOrderProductInfo(BeanConvertUtil.beanCovertMap(inoutOrderProductDto)),
                InoutOrderProductDto.class);

        return inoutOrderProducts;
    }


    @Override
    public int queryInoutOrderProductsCount(@RequestBody InoutOrderProductDto inoutOrderProductDto) {
        return inoutOrderProductServiceDaoImpl.queryInoutOrderProductsCount(BeanConvertUtil.beanCovertMap(inoutOrderProductDto));
    }

    @Override
    public int saveInoutOrderProducts(@RequestBody List<InoutOrderProductPo> inoutOrderProductPos) {
        Map info = new HashMap();
        info.put("inoutOrderProductPos", inoutOrderProductPos);
        return inoutOrderProductServiceDaoImpl.saveInoutOrderProducts(info);
    }

    @Override
    public List<InoutOrderProductDto> queryProductInoutCount(@RequestBody InoutOrderProductDto inoutOrderProductDto) {
        List<InoutOrderProductDto> inOrderProducts
                = BeanConvertUtil.covertBeanList(inoutOrderProductServiceDaoImpl.queryProductInCount(BeanConvertUtil.beanCovertMap(inoutOrderProductDto)),
                InoutOrderProductDto.class);

        List<InoutOrderProductDto> outOrderProducts
                = BeanConvertUtil.covertBeanList(inoutOrderProductServiceDaoImpl.queryProductInCount(BeanConvertUtil.beanCovertMap(inoutOrderProductDto)),
                InoutOrderProductDto.class);

        for (InoutOrderProductDto outOrderProductDto : outOrderProducts) {
            outOrderProductDto.setInCount(0);
            if (!hasInoutOrderProducts(inOrderProducts, outOrderProductDto)) {
                inOrderProducts.add(outOrderProductDto);
            }
        }

        for (InoutOrderProductDto inOrderProduct : inOrderProducts) {
            for (InoutOrderProductDto outOrderProduct : outOrderProducts) {
                if (!inOrderProduct.getValueId().equals(outOrderProduct.getValueId())) {
                    continue;
                }
                inOrderProduct.setOutCount(outOrderProduct.getOutCount());
            }
        }
        return inOrderProducts;
    }

    private boolean hasInoutOrderProducts(List<InoutOrderProductDto> inOrderProducts, InoutOrderProductDto outOrderProduct) {
        for (InoutOrderProductDto inoutOrderProductDto : inOrderProducts) {
            if (inoutOrderProductDto.getValueId().equals(outOrderProduct.getValueId())) {
                return true;
            }
        }

        return false;
    }

    public IInoutOrderProductServiceDao getInoutOrderProductServiceDaoImpl() {
        return inoutOrderProductServiceDaoImpl;
    }

    public void setInoutOrderProductServiceDaoImpl(IInoutOrderProductServiceDao inoutOrderProductServiceDaoImpl) {
        this.inoutOrderProductServiceDaoImpl = inoutOrderProductServiceDaoImpl;
    }
}
